<?php

namespace app\controller\admin\run;

use laytp\controller\Backend;

/**
 * Class Game
 * @package app\controller\admin\run
 */
class Proplevel extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $prop_model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'asc'];

    public function _initialize()
    {
        $this->model = new \app\model\admin\run\Proplevel();
        $this->prop_model = new \app\model\admin\run\Prop();
    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        } else {
            $limit = $this->request->param('limit', 10);
            $data  = $this->model->where($where)->group('prop_id,level')->order("prop_id desc,level asc")->paginate($limit)->toArray();

            $game = (new \app\model\admin\run\Game())->column('name','id');
            $prop = (new \app\model\admin\run\Prop())->column('name,icon','id');
            array_walk($data['data'],function(&$item,$key) use ($game,$prop){
                if(isset($game[$item['game_id']]))$item['game_name'] = $game[$item['game_id']];
                if(isset($prop[$item['prop_id']]))$item['prop_name'] = $prop[$item['prop_id']]['name'];
                if(isset($prop[$item['prop_id']]))$item['prop_icon'] = imgUrlToAbsolute($prop[$item['prop_id']]['icon']);
            });
        }
        return $this->success('数据获取成功', $data);
    }


    public function info()
    {
        $id   = $this->request->param('id');
        $detail = $this->model->find($id);

        $material_list = $this->model->where(array('prop_id' => $detail['prop_id'],'game_id' => $detail['game_id'],'level' => $detail['level']))->select();
        $info = $this->model->field('id,prop_id,game_id,level')->find($id);
        $info['prop_name'] = $this->prop_model->where(array('id'=> $info['prop_id']))->value('name');

        $materials = [];
        foreach ($material_list as $item){
            $materials[] =[
                'id'  =>$item['material_id'],
                'number'  =>$item['material_num'],
            ];
        }
        $info->material = $materials;

        return $this->success('获取成功', $info);
    }



    /**
     * 编辑材料
     * @return false|string|\think\response\Json
     */
    public function edit(){
        $post     = input("param.");
        $detail = $this->prop_model->find($post['prop_id']);
        empty($detail) && $this->error('数据不存在');
        $detail = $detail->toArray();

        $materialList = $post['material'];
        if(empty($materialList)) $this->error('素材不可为空');

        $insData = [];
        foreach ($materialList as $item){
            if(!empty($item['id']) && !empty($item['number'])){
                $insData[] = array(
                    'game_id' => $detail['game_id'],
                    'prop_id' => $detail['id'],
                    'level' => $post['level'],
                    'material_id' => $item['id'],
                    'material_num' => $item['number']
                );
            }
        }

        if(empty($insData)){
            $this->error('素材不可为空');
        }

        try {
            $delete_res = $this->model->where(array('prop_id' => $detail['id'],'game_id' => $detail['game_id'],'level' => $post['level']))->delete();
            if (!$delete_res) throw new \Exception('保存失败');
            $ins_res = $this->model->insertAll($insData);
            if (!$ins_res) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }

    }


}